package com.tencent.yumeko;

import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/producer")
public class MetadataProducerController {
    private static final Logger LOG = LoggerFactory.getLogger(MetadataProducerController.class);

    @Value("${server.port:0}")
    private int port;

    /**
     * Get information of producer.
     *
     * @return information of producer
     */
    @GetMapping("/info")
    public Map<String, String> info() {
        LOG.info("Metadata Service Producer [{}] is called.", port);

        // 从上下文中获取自定义元数据
        MetadataContext context = MetadataContextHolder.get();
        Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);

        customMetadataMap.forEach((key, value) -> {
            LOG.info("Custom Metadata (Key-Value): {} : {}", key, value);
        });

        return customMetadataMap;
    }
}
